package palmtop.com.palmtop.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.AwesomeTextView;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import palmtop.com.palmtop.R;
import palmtop.com.palmtop.model.Clue;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.FuncUtil;
import palmtop.com.palmtop.util.NetUtil;
import palmtop.com.palmtop.util.PlaybackDialogFragment;
import palmtop.com.palmtop.util.RecordingItem;
import palmtop.com.palmtop.util.ToastUtil;

public class CluedetailActivity extends BaseActivity {

    @BindView(R.id.mtitle)
    TextView mtitle;
    @BindView(R.id.reportman)
    AwesomeTextView reportman;
    @BindView(R.id.position)
    AwesomeTextView position;
    /*@BindView(R.id.cluetypename)
    AwesomeTextView cluetypename;*/
    @BindView(R.id.addtime)
    AwesomeTextView addtime;
    @BindView(R.id.cluecontent)
    AwesomeTextView cluecontent;

    Clue clue;
    @BindView(R.id.zphoto1)
    ImageView zphoto1;
    @BindView(R.id.zphoto2)
    ImageView zphoto2;
    @BindView(R.id.zphoto3)
    ImageView zphoto3;
    @BindView(R.id.zphoto4)
    ImageView zphoto4;
    @BindView(R.id.confirm)
    Button confirm;
    @BindView(R.id.reject)
    Button reject;
    @BindView(R.id.blay)
    LinearLayout blay;
    @BindView(R.id.v11)
    TextView v11;
    @BindView(R.id.status)
    AwesomeTextView status;
    @BindView(R.id.lay11)
    LinearLayout lay11;
    @BindView(R.id.play)
    Button play;
    MyApplication app;
    @BindView(R.id.lookmapBtn)
    Button lookmapBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cluedetail, Config.toptitle, 1);
        ButterKnife.bind(this);
        //x.view().inject(this);
        app = (MyApplication) getApplication();
        app.addActivity(CluedetailActivity.this);
        app.addActivityList(CluedetailActivity.this);
        initView();
    }

    private void initView() {
        mtitle.setText("线索详情");

        AppPreferences appPreferences = new AppPreferences(CluedetailActivity.this);
        String role_id = appPreferences.getString("role_id", "");

        clue = (Clue) getIntent().getSerializableExtra("clue");
        if(role_id.equals("73006b03b7f447bf953d065462e5b1ac")){
            FuncUtil.updateReadClue(String.valueOf(clue.getId()));
        }

        if(role_id.equals("d18ac0ef29ee477f8e3dd214245a1de5")&&clue.getStatus().equals("0")){

        }else{
            blay.setVisibility(View.GONE);
            v11.setVisibility(View.VISIBLE);
            lay11.setVisibility(View.VISIBLE);
        }
        path = Config.base_url + clue.getRecordurl();
        reportman.setText(clue.getReportman());
        position.setText(clue.getPosition());
//        cluetypename.setText(clue.getCluetypename());
        if (TextUtils.isEmpty(clue.getRecordurl())) {
            play.setEnabled(false);
        }
//        if (role_id.equals("73006b03b7f447bf953d065462e5b1ac")) {
            if (clue.getStatus().equals("0")) {
                status.setText("待确认");
            }
            if (clue.getStatus().equals("1")) {
                status.setText("已采纳");
            }
            if (clue.getStatus().equals("2")) {
                status.setText("未采纳");
            }
//        }
        addtime.setText(clue.getAddtime());
        cluecontent.setText(clue.getCluecontent());
        if (clue.getPhoto1() != null && !clue.getPhoto1().equals("")) {
            x.image().bind(zphoto1, Config.IMG_URL + clue.getPhoto1());
            FuncUtil.clickShowBigPic(Config.IMG_URL + clue.getPhoto1(), zphoto1, CluedetailActivity.this);
        }
        if (clue.getPhoto2() != null && !clue.getPhoto2().equals("")) {
            x.image().bind(zphoto2, Config.IMG_URL + clue.getPhoto2());
            FuncUtil.clickShowBigPic(Config.IMG_URL + clue.getPhoto2(), zphoto2, CluedetailActivity.this);
        }
        if (clue.getPhoto3() != null && !clue.getPhoto3().equals("")) {
            x.image().bind(zphoto3, Config.IMG_URL + clue.getPhoto3());
            FuncUtil.clickShowBigPic(Config.IMG_URL + clue.getPhoto3(), zphoto3, CluedetailActivity.this);
        }
        if (clue.getPhoto4() != null && !clue.getPhoto4().equals("")) {
            x.image().bind(zphoto4, Config.IMG_URL + clue.getPhoto4());
            FuncUtil.clickShowBigPic(Config.IMG_URL + clue.getPhoto4(), zphoto4, CluedetailActivity.this);
        }
    }

    @OnClick(R.id.confirm)
    public void onConfirmClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "clue/updateStatus");
        params.addBodyParameter("id", String.valueOf(clue.getId()));
        params.addBodyParameter("status", "1");
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    ToastUtil.show(CluedetailActivity.this, "确认成功");
                    finish();

                } else {
                    ToastUtil.show(CluedetailActivity.this, "确认失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    @OnClick(R.id.reject)
    public void onRejectClicked() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "clue/updateStatus");
        params.addBodyParameter("id", String.valueOf(clue.getId()));
        params.addBodyParameter("status", "2");
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result.equals("success")) {
                    ToastUtil.show(CluedetailActivity.this, "驳回成功");
                    finish();

                } else {
                    ToastUtil.show(CluedetailActivity.this, "驳回失败");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    private Callback.Cancelable cancelable;
    String path = "";
    /*@OnClick(R.id.play)
    public void onViewClicked() {
      // path = "http://192.168.3.111:8080/palmtop/uploads/beyond.mp3";
        final String paths = Environment.getExternalStorageDirectory()+File.separator+System.currentTimeMillis()+".mp3";
        RequestParams params = new RequestParams(path);
        params.setAutoResume(true);//设置是否在下载是自动断点续传
        params.setAutoRename(false);//设置是否根据头信息自动命名文件
        params.setSaveFilePath(paths);
        params.setExecutor(new PriorityExecutor(2, true));//自定义线程池,有效的值范围[1, 3], 设置为3时, 可能阻塞图片加载.
        params.setCancelFast(true);//是否可以被立即停止.
        //下面的回调都是在主线程中运行的,这里设置的带进度的回调
        cancelable = x.http().post(params, new Callback.ProgressCallback<File>() {
            @Override
            public void onCancelled(CancelledException arg0) {
                int i = 0;
//                Log.i("tag", "取消"+Thread.currentThread().getName());
            }

            @Override
            public void onError(Throwable arg0, boolean arg1) {
                int i = 0;
                Log.e("erroe-->0",arg0.getMessage());
                ToastUtil.show(getApplicationContext(),arg0.getMessage());
//                Log.i("tag", "onError: 失败"+Thread.currentThread().getName());
//                progressDialog.dismiss();
            }

            @Override
            public void onFinished() {
                int i = 0;
//                Log.i("tag", "完成,每次取消下载也会执行该方法"+Thread.currentThread().getName());
//                progressDialog.dismiss();
            }

            @Override
            public void onSuccess(File arg0) {
//                Log.i("tag", "下载成功的时候执行"+Thread.currentThread().getName());
                RecordingItem recordingItem = new RecordingItem();
                SharedPreferences sharePreferences = getSharedPreferences("sp_name_audio", MODE_PRIVATE);
                String filePath = "/sdcard/filesss/123.mp3";
                int elpased = Integer.valueOf(clue.getRecordlength());
                recordingItem.setFilePath(paths);
                recordingItem.setLength(elpased);
//        recordingItem.setLength((int)6);
                PlaybackDialogFragment fragmentPlay = PlaybackDialogFragment.newInstance(recordingItem);
                fragmentPlay.show(getSupportFragmentManager(), PlaybackDialogFragment.class.getSimpleName());

            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                int i = 0;
//                if (isDownloading) {
//                    progressDialog.setProgress((int) (current*100/total));
//                    Log.i("tag", "下载中,会不断的进行回调:"+Thread.currentThread().getName());
//                }
            }

            @Override
            public void onStarted() {
                int i = 0;
//                Log.i("tag", "开始下载的时候执行"+Thread.currentThread().getName());
//                progressDialog.show();
            }

            @Override
            public void onWaiting() {
                int i = 0;
//                Log.i("tag", "等待,在onStarted方法之前执行"+Thread.currentThread().getName());
            }

        });




       *//*String urlStr=Config.base_url+clue.getRecordurl();
        String path="file";
        String fileName="123.mp3";
        HttpDownloader httpDownloader = new HttpDownloader();
        httpDownloader.downlaodFile(urlStr,path,fileName);*//*



    }*/

    @OnClick(R.id.play)
    public void onViewClicked() {
        RecordingItem recordingItem = new RecordingItem();
        SharedPreferences sharePreferences = getSharedPreferences("sp_name_audio", MODE_PRIVATE);
        String filePath = Config.base_url + clue.getRecordurl();
        int elpased = Integer.valueOf(clue.getRecordlength());
        recordingItem.setFilePath(filePath);
        recordingItem.setLength(elpased);
//        recordingItem.setLength((int)6);
        PlaybackDialogFragment fragmentPlay = PlaybackDialogFragment.newInstance(recordingItem);
        fragmentPlay.show(getSupportFragmentManager(), PlaybackDialogFragment.class.getSimpleName());

    }

    @OnClick(R.id.lookmapBtn)
    public void onlookmapClicked() {
        Intent i = new Intent();
        if (!TextUtils.isEmpty(clue.getLatitude())) {
            i.putExtra("latitude", clue.getLatitude());
        }
        if (!TextUtils.isEmpty(clue.getLongitude())) {
            i.putExtra("longitude", clue.getLongitude());
        }
        i.setClass(this, LookmapActivity.class);
        startActivityForResult(i, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
